// CommonMoon.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "CommonMoon.h"
#include "tinyxml.h"
#include "kscconv.h"
#include "kscfilepath.h"
#include <string>

bool GetRawDataFromRes(
						const std::string& strId, 
						void** ppBuffer, 
						unsigned long& dwSize
						)
{
	bool retval = false;  
	HANDLE hFile = INVALID_HANDLE_VALUE;

	USES_CONVERSION;
	MGFilePath pathRes = MGFilePath::GetFilePath(NULL);  
	MGFilePath pathImages = pathRes.DirName();	
	pathImages.AddBackslash(); 
	std::wstring strXmlPath =  AnsiToUnicode(strId.c_str()); 

	pathImages.Append(strXmlPath.c_str()); 

	if (!ppBuffer)
		goto clean0;  

	hFile = CreateFile(pathImages, GENERIC_ALL, FILE_SHARE_READ, NULL,
		OPEN_EXISTING, FILE_ATTRIBUTE_TEMPORARY, NULL);
	if (INVALID_HANDLE_VALUE == hFile)
		goto clean0;

	DWORD dwSizeHight = 0;
	dwSize = GetFileSize(hFile, &dwSizeHight);

	*ppBuffer = new unsigned char[dwSize+1];
	if (!*ppBuffer)
		goto clean0;

	BOOL bRead = ReadFile(hFile, *ppBuffer, dwSize, &dwSizeHight, NULL);
	if (!bRead)
	{
		goto clean0;
	}

retval = true;


clean0:
	if (!retval)
	{
		if (ppBuffer)
		{
			if (*ppBuffer)
			{
				delete[] (*ppBuffer);
				*ppBuffer = NULL;
			}
		}
	}
	if (hFile != INVALID_HANDLE_VALUE)
	{
		CloseHandle(hFile);
		hFile = INVALID_HANDLE_VALUE;
	}

	return retval;
}

void FreeRawData(void* pBuffer)
{
	if (pBuffer)
	{
		delete[] (unsigned char*)pBuffer;
		pBuffer = NULL;
	}
}

bool TestXml1()
{
	bool retval = false;
	void* pBuffer = NULL;
	unsigned long dwBuffer = 0;
	TiXmlDocument xmlDoc;
	const TiXmlElement* pXmlChild = NULL;
	const TiXmlElement* pXmlItem = NULL;

	MGFilePath pathRes = MGFilePath::GetFilePath(NULL);  
	MGFilePath pathImages = pathRes.DirName();	
	pathImages.AddBackslash();  

	pathImages.Append(TEXT("strings.xml")); 


	std::wstring strPath = pathImages;
 
	if(!xmlDoc.LoadFile(UnicodeToAnsi(strPath).c_str()))
	//if (!xmlDoc.LoadBuffer((char*)pBuffer, (long)dwBuffer, TIXML_ENCODING_UTF8))
		goto clean0;

	pXmlChild = xmlDoc.FirstChildElement("strings");
	if (!pXmlChild)
		goto clean0;
 
	pXmlItem = pXmlChild->FirstChildElement("string");
	while (pXmlItem) 
	{
		std::string strId;
		std::string strValue;

		strId = pXmlItem->Attribute("id");
		strValue = pXmlItem->Attribute("value");

		if (strId.length() && strValue.length())
		{
			OutputDebugString(Utf8ToUnicode(strValue.c_str()).c_str());
			OutputDebugString(TEXT("\n"));
		}

		pXmlItem = pXmlItem->NextSiblingElement("string");
	}

	retval = true;

clean0:
	if (pBuffer)
	{
		FreeRawData(pBuffer);
	}

	return retval;
}


bool TestXml2()
{
	bool retval = false;
	void* pBuffer = NULL;
	unsigned long dwBuffer = 0;
	TiXmlDocument xmlDoc;
	const TiXmlElement* pXmlChild = NULL;
	const TiXmlElement* pXmlItem = NULL;

	if (!GetRawDataFromRes("xmls.xml", &pBuffer, dwBuffer))
		goto clean0;

	if (!xmlDoc.LoadBuffer((char*)pBuffer, (long)dwBuffer, TIXML_ENCODING_UTF8))
		goto clean0;

	pXmlChild = xmlDoc.FirstChildElement("xmls");
	if (!pXmlChild)
		goto clean0;

	pXmlItem = pXmlChild->FirstChildElement("xml");
	while (pXmlItem) 
	{
		std::string strId;
		std::string strValue;

		strId = pXmlItem->Attribute("id");
		strValue = pXmlItem->Attribute("path");

		if (strId.length() && strValue.length())
		{
			OutputDebugString(Utf8ToUnicode(strValue.c_str()).c_str());
			OutputDebugString(TEXT("\n"));
		}

		pXmlItem = pXmlItem->NextSiblingElement("xml");
	}

	retval = true;

clean0:
	if (pBuffer)
	{
		FreeRawData(pBuffer);
	}

	return retval;
}

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	TestXml1();
	TestXml2();

	return 0;
}
